const http = require("http");
const fs = require("fs");
const path = require("path");
var file = process.argv[2];
var filePath = path.join(__dirname,"/"+file);

http.createServer((req,res)=>{
    if(fs.existsSync(filePath)){
        var fd = fs.openSync(filePath,"r")
        var fileObj = fs.statSync(filePath);
        var buf = Buffer.alloc(fileObj.size);
        fs.read(fd,buf,0,fileObj.size,0,(err,bytesRead,buffer)=>{
            if(err){
                console.log(err);
            }
            res.end(buffer.toString("utf-8"));
            
        })
        fs.close(fd,err=>{
            if(err){
                console.log(err);
            }
        })
    }
    else{
        console.log("该文件不存在");
    }
    

}).listen(8081);
console.log("server is listening 8081");

